// pages/explore/index.js
import http from '../../utils/http.js'
let page = 0
Page({
    data: {
        course:[],
        cityName:'重庆市',
        sId:'', //设置菜单滚动
        cateList:[],
        tabIdx:0,
        tabShow:true,
        active:'全部' //二级菜单的name 
    },
    onLoad(options) {
        //分类数据
        getApp().fly.get('classes/ReactCategory').then(res=>{
            console.log(res);
            let {results} = res.data
            let parentArr = results.filter(item=>item.parentId=='0-0')
            parentArr.forEach(item=>{
                let children = results.filter(child=>item.objectId==child.parentId)
                children.unshift({name:'全部',objectId:'all'})
                item.children = children
            })
            this.setData({
                cateList:parentArr
            })
            //数据列表        
            this.fetchData('全部')
        })
        
        
    },
    handleDetail(ev){
        console.log('事件对象',ev);
        let {id,item} = ev.currentTarget.dataset
        //打开新页面
        wx.navigateTo({
          url: `/pages/detail/index?id=${id}&num=123`,
        })
    },
    handleCityList(ev){
        console.log('子父通信数据',ev);
        // this  指向Page对象
        wx.navigateTo({
          url: '/pages/city/index',
          events:{
              changeCity:(name)=>{
                // console.log(name);
                this.setData({
                    cityName:name
                })
              }
          }
        })
    },
    //主菜单
    handleTab(ev){
        let {idx} = ev.currentTarget.dataset
        let sId = `tab-${idx-1}`
        console.log(sId);
        this.setData({
            tabIdx:idx,
            sId,
            tabShow:false,
            course:[],
            active:'全部'
        })
        page=0
        setTimeout(()=>{
            this.setData({tabShow:true})
        },10)

        //重新发起请求
        this.fetchData('全部')
    },
    //子菜单
    handleChange(ev){
        console.log(ev);
        let {name} = ev.detail
        this.setData({active:name,course:[]})
        page = 0
        this.fetchData()
    },
    fetchData(){ //封装的列表数据请求方法
        let {course,cateList,tabIdx,active} = this.data
        let cateLv1 = cateList[tabIdx].name //一级类目
        let where = {cateLv1}
        if(active!='全部'){
            where.cateLv2 = active
        }
        let skip = page*10
        getApp().fly.get('classes/ReactCourse',{
            where,
            limit:10,
            skip
        }).then(res=>{
            wx.stopPullDownRefresh() //关闭下拉刷新动画
            let {results} = res.data
            if(results.length){
                this.setData({
                    course:[
                        ...course,
                        ...results
                    ]
                })
                page++
                return
            }
            wx.showToast({
              title: '没有更多了',
              icon:'none'
            })
        })
    },
    onReachBottom(){
        console.log('触底了',this.data.active);
        this.fetchData()
    },
    onPullDownRefresh(){
        console.log('下拉刷新');
        this.setData({
            course:[],
            tabIdx:0,
            active:'全部',
            sId:'tab-0',
            tabShow:false
        })
        page=0
        setTimeout(()=>{
            this.setData({tabShow:true})
        },10)
        this.fetchData()
    }
})